package com.examplemartin.jpademo.service;

import com.examplemartin.jpademo.dao.RepoInfoDao;
import com.examplemartin.jpademo.pojo.po.good.GoodPO;
import com.examplemartin.jpademo.pojo.po.stock.RepoInfoPO;
import com.examplemartin.jpademo.pojo.po.stock.StockPO;
import com.examplemartin.jpademo.pojo.vo.purchase.RepoinfoVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RepoInfoService {
    @Resource
    private RepoInfoDao repoInfoDao;

    @Resource
    private GoodService goodService;

    /**
     * 查询
     * @return
     */
    // 查询所有
    public List<RepoInfoPO> findAll(){
        return repoInfoDao.findAll();
    }
    // 查询 -- 按id
    public RepoInfoPO findById(Long id){
        return repoInfoDao.findById(id).get();
    }

    // 查询 -- 根据商品和仓库
    public List<RepoInfoPO> findAllByStockPOAndGoodPO(StockPO stockPO, GoodPO goodPO){
        return repoInfoDao.findAllByStockPOAndGoodPO(stockPO,goodPO);
    }

    // 查询 -- 需要补货
    public List<RepoInfoPO> findAllNeedReplenish(){
        return repoInfoDao.findAllNeedReplenish();
    }

    /**
     * 保存
     */
    public RepoInfoPO save(RepoInfoPO repoInfoPO){
        return repoInfoDao.save(repoInfoPO);
    }

    public List<RepoinfoVO> findAllGroupByGoodId(){
        List<Object[]> allGroupByGoodId = repoInfoDao.findAllGroupByGoodId();
        ArrayList<RepoinfoVO> repoinfoVOS = new ArrayList<>();
        for (Object[] objects : allGroupByGoodId) {
            RepoinfoVO repoinfoVO = new RepoinfoVO();
            // 商品
            Object obj00 = objects[0];
            GoodPO byId = goodService.findById((Long) obj00);
            repoinfoVO.setGoodPO(byId);

            // 库存
            Object obj01 = objects[1];
            int goodcount = ((Long) obj01).intValue();
            repoinfoVO.setGoodcount(goodcount);

            // 备货
            Object obj02 = objects[2];
            int goodspare = ((Long) obj02).intValue();
            repoinfoVO.setGoodspare(goodspare);


            repoinfoVOS.add(repoinfoVO);
        }
        return repoinfoVOS;
    }

    // 删除条目
    public void deleteById(Long id){
        repoInfoDao.deleteById(id);
    }

    // 补货信息
    public List<RepoInfoPO> findByShortage(){
      return   repoInfoDao.findByShortage();
    }

    public List<RepoInfoPO> findAllByGoodPO(GoodPO goodPO) {
        return repoInfoDao.findAllByGoodPO(goodPO);
    }

  public List<RepoInfoPO> findAllByStockPO(StockPO byId) {
        return repoInfoDao.findAllByStockPO( byId);
  }
}
